import java.util.Arrays;

public class MyArrayList implements IList{

    private int[] arr;
    private int usesize;

    private static final int P = 10;

    public MyArrayList() {
        arr = new int[P];
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public void add(int data) {
        if(isFull()) {
            grow();
        }
        arr[usesize] = data;
        usesize++;

    }



    private void grow() {
        arr = Arrays.copyOf(arr, 2 * arr.length);
    }

    @Override
    public boolean isFull() {
        return usesize == this.arr.length;
    }

    @Override
    public void add(int pos, int data) {


//        if(pos < 0 || pos > usesize) {
//            throw new PosException("插入位置有误");
//        }

        try{
            check2(pos);
            if(isFull()) {
                grow();
            }
            for (int i = usesize - 1; i >= pos ; i--) {
                arr[i + 1] = arr[i];
            }
            arr[pos] = data;
            usesize++;
        } catch (PosException e) {
            System.out.println("插入位置有误");
            e.printStackTrace();
        }

    }


    @Override
    public boolean contains(int toFind) {

        for (int i = 0; i < usesize; i++) {
            if(toFind == arr[i]) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usesize; i++) {
            if(toFind == arr[i]) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        /*if(usesize == 0) {
            throw new EmptyException("数组为空");
        }
        if(pos < 0 || pos >= usesize) {
            throw new PosException("插入位置有误");
        }*/

        try{
            empty();
            check(pos);
            return arr[pos];

        } catch (PosException e) {
            e.printStackTrace();
        } catch (EmptyException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public void set(int pos, int value) {

/*        if(usesize == 0) {
            throw new EmptyException("数组为空");
        }
        if(pos < 0 || pos > usesize) {
            throw new PosException("插入位置有误");
        }*/

        try{
            empty();
            check(pos);
            arr[pos] = value;

        } catch (PosException e) {
            e.printStackTrace();
        } catch (EmptyException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void remove(int toRemove) {

//        if(usesize == 0) {
//            throw new EmptyException("数组为空");
//        }
//        for (int i = 0; i < usesize; i++) {
//            if(arr[i] == toRemove) {
//                for (int j = i; j < usesize - 1; j++) {
//                    arr[j]  = arr[j + 1];
//                }
//            }
//        }

        try{
            empty();
            int p = indexOf(toRemove);
            if(p == -1) {
                return;
            }
            for (int j = p; j < usesize - 1; j++) {
                arr[j] = arr[j + 1];

                usesize--;
            }
        } catch (EmptyException e) {
            System.out.println("数组为空");
            e.printStackTrace();
        }


    }

    @Override
    public int size() {
        return usesize;
    }

    @Override
    public void clear() {
//
//        for (int i = 0; i < usesize; i++) {
//            arr[i] = null;
//        }
        usesize = 0;

    }

    @Override
    public void display() {

        for (int i = 0; i < usesize; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    private void check(int pos) throws PosException{
        if(pos < 0 || pos >= usesize ) {
            throw new PosException("输入位置有误");
        }
    }

    private void check2(int pos) throws PosException{
        if(pos < 0 || pos > usesize ) {
            throw new PosException("输入位置有误");
        }
    }

    private void empty() throws EmptyException{
        if(usesize == 0) {
            throw new EmptyException("数组为空");
        }
    }
}
